<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>系统用户管理</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 分割线 -->
        <el-divider></el-divider>

        <el-form :inline="true" :model="form" class="demo-form-inline" @submit.native.prevent>
            <el-form-item label="用户名">
                <el-input @keyup.native.enter="onSubmit" v-model="form.name" placeholder="输入用户名:模糊查询"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">查询</el-button>
            </el-form-item>
        </el-form>
        <!-- 分割线 -->
        <el-divider content-position="left">用户列表</el-divider>

        <!-- 用户表格 -->
        <el-table :data="userData.result" border style="width: 100%">
            <el-table-column prop="ID" label="编号" width="80">
            </el-table-column>
            <el-table-column prop="username" label="用户名" width="130">
            </el-table-column>
            <el-table-column prop="password" label="用户密码" width="120">
                <template slot-scope="scope">
                    <!-- <span v-if="scope.row.isCollapse">{{ scope.row.password }}</span>
                    <span v-else>{{ repwd }}</span>
                    <i v-show="!scope.row.isCollapse" class="el-icon-turn-off" @click="changeShow"
                        style="float:right;font-size: 20px;cursor:pointer;"></i>
                    <i v-show="scope.row.isCollapse" class="el-icon-open" @click="changeShow"
                        style="float:right;font-size: 20px;cursor:pointer;color:green;"></i> -->
                    <span>{{ scope.row.password }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="role" label="角色权限" width="120">
            </el-table-column>
            <el-table-column label="注册时间" width="200">
                <template slot-scope="scope">
                    {{ $moment(scope.row.addtime * 1000).format('YYYY-MM-DD') }}
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作">
                <template slot-scope="scope">
                    <el-row>
                        <el-button type="info" icon="el-icon-user" circle @click="changeInfo(scope.row)">
                        </el-button>
                        <el-button type="success" icon="el-icon-picture-outline" circle></el-button>
                        <el-button type="warning" icon="el-icon-edit" circle></el-button>
                        <el-button type="danger" icon="el-icon-delete" circle @click="delUser(scope.row.ID)">
                        </el-button>
                    </el-row>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页器 -->
        <el-pagination background layout="->, prev, pager, next, total, jumper" :total="userData.total"
            :page-size="userData.pagesize" :current-page="userData.page" style="margin:10px;"
            @current-change="pageChange">
        </el-pagination>

        <el-dialog title="修改用户信息" :visible.sync="dialogFormVisible">
            <el-form :model="user" label-width="100px" :rules="rules" ref="userinfo">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="user.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="user.password" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitForm('userinfo')">修改</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            dialogFormVisible: false,
            form: {
                name: '' // 模糊搜索值
            },
            inputValue: '',
            user: {
                username: '',
                password: ''
            },
            userData: {
                page: 1,
                pagesize: 5,
                result: []
            },
            rules: {
                username: [{ required: true, message: '不能为空', trigger: 'blur' }],
                password: [{ required: true, message: '不能为空', trigger: 'blur' }]
            }
        }
    },
    methods: {
        pageChange(page) {
            //分页事件
            if (this.inputValue != '') {
                let param = { name: this.form.name, page: page, pagesize: this.moviesData.pagesize }
                this.$http.userApi.queryByNameLike(param).then(res => {
                    this.userData = res.data.data;
                })
            } else {
                let param = { page: page, pagesize: this.userData.pagesize }
                this.$http.userApi.queryAllUser(param).then(res => {
                    this.userData = res.data.data
                })
            }


        },
        onSubmit() {
            // 模糊查询事件
            if (this.form.name == '') return
            this.$http.userApi.queryLike({ name: this.form.name }).then(res => {
                console.log(res);
                this.userData.result = res.data.data
            })
        },
        changeInfo(row) {
            this.dialogFormVisible = true
            console.log(row);
        },

        delUser(id) {
            // 删除事件
            this.$confirm('此操作将永久删除此用户, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http.userApi.delete({ id }).then(res => {
                    if (res.data.code == 200) {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.$http.userApi.queryAllUser(this.userData).then(res => {
                            this.userData = res.data.data;
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        }
    },
    mounted() {
        this.$http.userApi.queryAllUser(this.userData).then(res => {
            this.userData = res.data.data;
        });
    },
}
</script>

<style lang="scss" scoped>
.view {
    border: none;

}
</style>